package com.yiren.dbaa.module.practice_set.site.view.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.module.practice_set.site.contract.OpenSiteRuleContract;
import com.yiren.dbaa.module.practice_set.site.presenter.OpenSiteRulePresenter;
import com.yiren.dbaa.module.practice_set.site.view.adapter.OpenSiteRuleAdapter;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/9/27 10:56
 * @description: 已公开教练规则
 */
public class OpenSiteRuleActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.iv_tip)
    ImageView ivTip;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.lv_open_site_rule)
    ListView lvOpenSiteRule;

    private OpenSiteRuleAdapter openSiteRuleAdapter;
    private OpenSiteRuleContract.Presenter openSiteRulePresenter;
    private UserParamConfigBean userParamConfigBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_siterule);
        ButterKnife.bind(this);
        tvTitle.setText("已公开给教练的考规");
        init();

    }

    private void init() {
        openSiteRulePresenter = new OpenSiteRulePresenter(this);
        userParamConfigBean = LoadConfigApi.loadUserParamConfig(this);
        LoadDialog.show(this, "正在加载数据...");
        openSiteRulePresenter.loadOpenSiteRule(new CommonCallBack() {
            @Override
            public void success(Object result) {
                LoadDialog.dismiss(OpenSiteRuleActivity.this);
                List<SiteEntity> siteList = (List<SiteEntity>) result;
                if (siteList.size() == 0) {
                    lvOpenSiteRule.setVisibility(View.GONE);
                    llNoData.setVisibility(View.VISIBLE);
                    tvTips.setText("暂无已公开的考规数据!");
                    ivTip.setImageResource(R.mipmap.nositerule_bg);
                    return;
                }
                openSiteRuleAdapter = new OpenSiteRuleAdapter(OpenSiteRuleActivity.this, siteList);
                lvOpenSiteRule.setAdapter(openSiteRuleAdapter);
                openSiteRuleAdapter.setOnManageClickListener(position -> {
                    if (siteList.get(position).getState() == 1) {
                        CommonDialog.showTwoBtn(OpenSiteRuleActivity.this, "操作确认", "停用后此考场新用户不能下载使用，已下载用 户不受影响，确定要停用吗？", "取消", "停用", (dialog, which) -> {
                            if (which == -2) {
                                dialog.dismiss();
                            } else {
                                openSiteRulePresenter.setSiteRuleState(0, userParamConfigBean.getUserInfo().getId().toString(), siteList.get(position).getRuleVer(), siteList.get(position).getSiteCode(), new CommonCallBack() {
                                    @Override
                                    public void success(Object result) {
                                        openSiteRuleAdapter.notifyDataSetChanged();
                                        siteList.get(position).setState(0);
                                    }

                                    @Override
                                    public void fail(String error) {
                                        MessageBox.show(OpenSiteRuleActivity.this, "设置失败!");
                                    }
                                });
                                dialog.dismiss();
                            }
                        });
                    } else {
                        CommonDialog.showTwoBtn(OpenSiteRuleActivity.this, "操作确认", "启用后此考规用户可以下载使用,确定要启用吗？", "取消", "启用", (dialog, which) -> {
                            if (which == -2) {
                                dialog.dismiss();
                            } else {
                                openSiteRulePresenter.setSiteRuleState(1, userParamConfigBean.getUserInfo().getId().toString(), siteList.get(position).getRuleVer(), siteList.get(position).getSiteCode(), new CommonCallBack() {
                                    @Override
                                    public void success(Object result) {
                                        siteList.get(position).setState(1);
                                        openSiteRuleAdapter.notifyDataSetChanged();
                                    }

                                    @Override
                                    public void fail(String error) {
                                        MessageBox.show(OpenSiteRuleActivity.this, "设置失败!");
                                    }
                                });
                                dialog.dismiss();
                            }
                        });
                    }
                });
            }

            @Override
            public void fail(String error) {
                lvOpenSiteRule.setVisibility(View.GONE);
                llNoData.setVisibility(View.VISIBLE);
                tvTips.setText("加载失败，请连接网络后重试!");
                ivTip.setImageResource(R.mipmap.network_no);
                MessageBox.show(OpenSiteRuleActivity.this, error);
            }
        });
    }

    @OnClick({R.id.iv_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

}
